<template>
	<view>
		<an-nav-bar title="我的地址"> </an-nav-bar>
		<view class="list" v-if="csListArrl.length != 0">
			<view v-for="(item,index) in csListArrl" :key="index" :data-index="index" class="order-item"
				@touchstart="drawStart" @touchmove="drawMove" @touchend="drawEnd" :style="'right:'+item.right+'rpx'"
				@click="onAds(item)">
				<view class="content">
					<view class="info ">
						<view class="ads flex">
							<text class="area">{{item.title}} </text>
							<text class="label" v-if="item.defaultFlag == '1'">默认</text>
						</view>
						<view class="contact">{{item.concatName}}{{item.sex == 1 ?'(先生)':'（女士）'}} +86{{item.mobile}}
						</view>
					</view>
					<view class="edit" @click.stop="onEdit(item)"></view>
				</view>
				<view class="remove" @click.stop="delData(item)">删除</view>
			</view>
		</view>
		<view class="no-address" v-if="csListArrl.length == 0">
			<view class=""><i class=""></i>
				<view class="text">增加新地址</view>
			</view>
		</view>
		<view class="bar flex_center">
			<button class="flex_center" @click="gotoUrl">
				<i></i>
				<span>增加新地址</span>
			</button>
		</view>
		<lw-message ref="lwMessage" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				delBtnWidth: 120,
				//列表数据，可根据自己的业务获取
				csListArrl: [],
				isRefresh: false,
				tId: ""
			};
		},
		onLoad(option) {
			if (option.tId) {
				this.tId = option.tId
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			onAds(item) {
				if (this.tId) {
					let pages = getCurrentPages() // 获取页面栈
					console.log(11111)
					if (pages.length > 1 && pages[pages.length - 2].route == "pages/submitOrder/submitOrder") {
						let prePage = pages[pages.length - 2] //获取上一页
						prePage.$vm.setAddress(item)
					}
					// 返回上一页 delta返回的页面数 如果delta大于现有页面数，则返回首页
					uni.navigateBack({
						delta: 1
					})
				}
			},
			getList() {
				this.$http.get('/app/customer/customerAddress/list').then(res => {
					this.csListArrl = res.rows
				})
			},
			onEdit(item) {
				uni.navigateTo({
					url: '/pages/my/addressCreate?id=' + item.id
				})
			},
			gotoUrl() {
				uni.navigateTo({
					url: '/pages/my/addressCreate'
				})
			},
			//开始触摸滑动
			drawStart(e) {
				var touch = e.touches[0];
				this.startX = touch.clientX;
			},
			//触摸滑动
			drawMove(e) {
				for (var index in this.csListArrl) {
					this.$set(this.csListArrl[index], 'right', 0);
				}
				var touch = e.touches[0];
				var item = this.csListArrl[e.currentTarget.dataset.index];
				var disX = this.startX - touch.clientX;
				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.$set(this.csListArrl[e.currentTarget.dataset.index], 'right', disX * 2);
				} else {
					this.$set(this.csListArrl[e.currentTarget.dataset.index], 'right', 0);
				}
			},
			//触摸滑动结束
			drawEnd(e) {
				var item = this.csListArrl[e.currentTarget.dataset.index];
				if (item.right >= this.delBtnWidth / 2) {
					this.$set(this.csListArrl[e.currentTarget.dataset.index], 'right', this.delBtnWidth);
				} else {
					this.$set(this.csListArrl[e.currentTarget.dataset.index], 'right', 0);
				}
			},
			//删除方法
			delData(item) {
				let self = this
				uni.showModal({
					title: '提示',
					content: "确认删除该地址吗？",
					success: function(res) {
						if (res.confirm) {
							self.$http.delete('/app/customer/customerAddress/' + item.id).then(res => {
								self.getList()
								self.$refs.lwMessage.show({
									id: "",
									msg: '删除成功',
									icon: "success",
								})
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.no-address {
		width: 100%;
		margin-top: 152px;

		>view {
			i {
				float: left;
				width: 100%;
				height: 146px;
				background: url(/static/icon_null.png) no-repeat 50%;
				background-size: 251px 146px;
			}

			.text {
				float: left;
				width: 100%;
				color: #999;
				font-size: 10px;
				text-align: center;
				margin-top: 10px;
			}
		}
	}

	.bar {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		background: #fff;
		box-shadow: 0 0 10rpx 0 rgba(0, 0, 0, .1);

		button {
			width: 690rpx;
			height: 80rpx;
			background: #aa8050;
			border-radius: 40rpx;
			color: #fff;
			font-size: 30rpx;

			i {
				display: block;
				width: 28rpx;
				height: 28rpx;
				position: relative;
				background: url(/static/add.png) no-repeat;
				background-size: 100% auto;
				margin-right: 6rpx;
			}
		}
	}

	.list {
		width: 100%;
		padding-bottom: 120rpx;
	}

	.order-item {
		height: 120rpx;
		background: #fff;
		width: 100%;
		display: flex;
		position: relative;
		margin: 10rpx auto;
		align-items: right;
		flex-direction: row;

		.remove {
			margin-left: -5%;
			position: absolute;
			top: 0;
			right: -120rpx;
			display: flex;
			z-index: 999;
			width: 120rpx;
			height: 120rpx;
			background: red;
			display: inline-block;
			vertical-align: middle;
			color: #fff;
			text-align: center;
			font-size: 26rpx;
			line-height: 120rpx;
		}
	}

	.content {
		width: 100%;
		height: 120rpx;
		box-sizing: border-box;
		padding: 0 50rpx 0 30rpx;
		display: inline-block;
		vertical-align: middle;
		overflow: hidden;

		.edit {
			display: inline-block;
			vertical-align: middle;
			width: 34rpx;
			height: 34rpx;
			background: url(/static/edit.png) no-repeat;
			background-size: 100% auto;
		}

		.info {
			width: 580rpx;
			height: 120rpx;
			display: inline-block;
			vertical-align: middle;

			.ads {
				color: #333;
				font-size: 28rpx;
				margin: 20rpx auto 6rpx;

				.area {
					display: block;
					max-width: 400rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.label {
					display: inline-block;
					height: 30rpx;
					background: #81c935;
					border-radius: 4rpx;
					color: #fff;
					font-size: 20rpx;
					line-height: 30rpx;
					margin-left: 20rpx;
					padding: 0 10rpx;
				}
			}

			.contact {
				color: #999;
				font-size: 24rpx;
			}
		}
	}
</style>
